<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // reduce() 方法对数组中的每个元素按序执行一个由您提供的 reducer 函数，
        // 每一次运行 reducer 会将先前元素的计算结果作为参数传入，最后将其结果汇总为单个返回值。
        const array1 = [1, 2, 3, 4];
        //accumulator 上次的值 currentValue 当前的值   initialValue 初始值
        // 0 + 1 + 2 + 3 + 4
        const initialValue = 0;
        const sumWithInitial = array1.reduce(
            (accumulator, currentValue) => accumulator + currentValue,
            initialValue
        );

        console.log(sumWithInitial)






        let arr=[{
            names:"aa",
            price:1000
        },
        {
            names:"bb",
            price:1000
        },
        {
            names:"cc",
            price:1000
        },
        {
            names:"dd",
            price:1000
        }]
        arr.forEach(element => {//遍历数组
            console.log(element);
            for (const key in element) {//遍历数组里的对象
                console.log(key);
                console.log(element[key]);
            }
        });
        const index = 0;
        const values = arr.reduce(
            (accumulator, currentValue) => accumulator + currentValue.price,
            index
        );

        console.log(values)//4000
    </script>
</body>

</html>